﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace PreContractWeb.Task
{
    public partial class ImportTranferStatus : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.tabDocument.Attributes.Add("onSelectedIndexChange", "ResizeDivForScroll('divGrid');ResizeDivForScroll('divGridTwo');");


        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {

            if (this.uplFile.FileName == null || this.uplFile.FileName.Trim() == "")
            {

                ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.GetType(), "", "<script>alert('" + "请选择需要上传的文件.\\nPlease select the file to upload." + "')</script>", false);
                return;
            }

            if (System.IO.Path.GetExtension(this.uplFile.FileName).ToLower() != ".xls")
            {
                ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.GetType(), "", "<script>alert('" + "上传文件的扩展名必须是xls.\\n\\nThe upload file extension must be xls." + "')</script>", false);
                return;
            }

            string filePath = BEGDC.MIS.Office.Configuration.UploadPath + uplFile.FileName;

            this.uplFile.SaveAs(filePath);

            try
            {

                BEGDC.MIS.Office.IOffice ioffice = (BEGDC.MIS.Office.IOffice)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_ContractTransferData_Service];
                DataSet dsUpload = ioffice.GetDataFromFile(filePath);

                System.Data.DataTable dtUpload_File = null;
                if (dsUpload != null && dsUpload.Tables.Count > 0 && dsUpload.Tables[0].Rows.Count > 0)
                {
                    dtUpload_File = dsUpload.Tables[0];
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.GetType(), "", "<script>alert('EXCEL中没有数据.\\nNo data in excel.')</script>", false);

                    return;
                }

                BEGDC.PreContract.BLL.ITempContractList tempContractservice = (BEGDC.PreContract.BLL.ITempContractList)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_TempContractList_Service];
                string strGuid = tempContractservice.SaveContractListByUpload(dtUpload_File);


                string url = System.Configuration.ConfigurationManager.AppSettings["Lotus_URL_Transformation"];

                string strRet = string.Empty;

                url = string.Format(url, strGuid);

                bool bRet = LotusDataRequester.GetDataByAuthentication(url, out strRet);

                if (!bRet)
                {
                    ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1,this.GetType(), "", "<script>alert('调用 Lotus 服务错误, 请确认服务地址正确.\\nError when calling to the Lotus\\'s Server, please check the server url.')</script>",false);
                    BaseFunction.CreateLog(BEGDC.PreContract.BLL.OperationLog.OperationType.ImportPreContract, strRet, User.Identity.Name);
                }

                DataSet ds = new DataSet();

                System.IO.StringReader reader = new System.IO.StringReader(strRet);

                ds.ReadXml(reader);

                System.Data.DataTable dtUpload = ds.Tables["record"];
                
                System.Data.DataTable dtUpdated = null;
                System.Data.DataTable dtMissing = null;
                System.Data.DataTable dtPartlyToFormalMultiBD = null;//added by yuliang 2010-5-25 用于存放跨BD部分转正信息
                System.Data.DataTable dtDiffBD = null;

                //update Orion Date
                if (dtUpload != null)
                {
                    foreach (System.Data.DataRow dr in dtUpload_File.Rows)
                    {
                        dtUpload.DefaultView.RowFilter = string.Format("FormalContractNo = '{0}'", dr[0].ToString());

                        foreach (System.Data.DataRow dr_Lotus in dtUpload.Rows)
                        {
                            if (Convert.ToString(dr_Lotus[0]) == Convert.ToString(dr[0]))
                            dr_Lotus["TransferDate"] = dr[1];
                        }
                    }
                }
                dtUpload.AcceptChanges();

                BEGDC.PreContract.BLL.IPreContractInfo contractservice = (BEGDC.PreContract.BLL.IPreContractInfo)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_PreContractInfo_Service];
                string strMessage = string.Empty;
                contractservice.UploadContractTransfer(dtUpload, out dtMissing, out dtUpdated, out dtPartlyToFormalMultiBD, out dtDiffBD, out strMessage);

                if (strMessage != string.Empty)
                {
                    ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.GetType(), "", strMessage, false);
                    //ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1,this.GetType(), "", strMessage);
                    return;
                }

                gvMain.DataSource = dtMissing;
                gvMain.DataBind();
                this.totalSpan.InnerText = dtMissing.Rows.Count.ToString();
                gvUpdated.DataSource = dtUpdated;
                gvUpdated.DataBind();
                this.totalSpan_Updated.InnerText = dtUpdated.Rows.Count.ToString();

                gvPartlyToFormalMultiBD.DataSource = dtPartlyToFormalMultiBD;
                gvPartlyToFormalMultiBD.DataBind();
                this.totalSpan_PartlyToFormalMultiBD.InnerText = dtPartlyToFormalMultiBD.Rows.Count.ToString();

                gvdiffBD.DataSource = dtDiffBD;
                gvdiffBD.DataBind();
                this.totalSpan_gvdiffBD.InnerText = dtDiffBD.Rows.Count.ToString();


                BaseFunction.CreateLog(BEGDC.PreContract.BLL.OperationLog.OperationType.ImportTransfer, BEGDC.PreContract.BLL.OperationLog.OperationType.ImportTransfer.ToString(), User.Identity.Name);
            }
            catch (Exception ex)
            {

                ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1,this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>",false);
            }

        }

        protected void gvMain_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }


        protected void gvUpdated_RowDataBound(object sender, GridViewRowEventArgs e)
        {


        }

        protected void gvPartlyToFormalMultiBD_RowDataBound(object sender, GridViewRowEventArgs e)
        {


        }

        protected void pvdiffBD_RowDataBound(object sender, GridViewRowEventArgs e)
        {


        }

        
    }
}
